#!/bin/bash
#创建定时备份
#crontab -e "20 1 * * * /usr/HeathyOfAged/backupMySqlDB.sh"
#备份保存路径
backup_dir=/usr/HeathyOfAged/mysqlbackup
#日期 
dd=`date +%Y%m%d`
#备份工具 
tool=mysqldump
#用户名 
username=root
#密码 
password=Inspur123!
#将要备份的数据库 
database_name=health_kyzhkj
#发送备份数据到指定邮箱
sendto=lil@inspursc.com
backup_file=$backup_dir/$database_name-$dd.sql.gz
if [ -f $backup_file ];then
echo "Reject duplicate create $backup_file" >> $backup_dir/log.txt 
else
#执行备份
#mysqldump -u root -pInspur123! health_kyzhkj --log-error=/usr/HeathyOfAged/mysqlbackup/mysqldump_health_kyzhkj-`date +%Y%m%d`-error_log.err > /usr/HeathyOfAged/mysqlbackup/health_kyzhkj-`date +%Y%m%d`.sql
mysqldump -u $username -p$password $database_name --log-error=$backup_dir/mysqldump_$database_name-$dd-error_log.err | gzip > $backup_file
#find $backup_dir/ -mtime +7 -name '*[2-9|0].sql' -exec rm -f {} \;
find $backup_dir/ -mtime +60 -name '*.sql' -exec rm -f {} \;
find $backup_dir/ -mtime +60 -name '*.sql.gz' -exec rm -f {} \;
#写创建备份日志 
echo "create $backup_file" >> $backup_dir/log.txt 
fi
if [ `date +%w` -eq "6" ];then
echo "$dd 康益健康系统数据库备份文件，保密内容。" | mailx -s "$dd 数据库备份文件" -a $backup_file $sendto
fi
